﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.Pages.SCS.Model;
using Twi.COMMON.Core;

namespace Twi.Pages.SCS.BLL
{
    public class _SingleRecord
    {
        private static readonly _SingleRecord instance = new _SingleRecord();
        private _SingleRecord() { }
        public static _SingleRecord Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 上下班打卡操作
        /// </summary>
        /// <returns>打卡时刻</returns>
        public string CheckInOut(string userID,string userRealName)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                TSingleRecord entity = new TSingleRecord();
                entity.FID = Guid.NewGuid();
                entity.FUserID = new Guid(userID);
                entity.FUserRealName = userRealName;
                entity.FDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FDayOfWeek = TwiDateHelper.GetCNDayOfWeek(DateTime.Today.DayOfWeek);
                db.TSingleRecord.InsertOnSubmit(entity);
                db.SubmitChanges();
                return entity.FDate + " " + entity.FTime;
            }
        }

        /// <summary>
        /// 获取指定用户当前阶段过几次卡【当天打卡时间范围内】
        /// </summary>
        /// <returns></returns>
        public int GetStageCheckedCount(string strUserID)
        {
            string beforeTime = _KindlyTip.Instance.GetBeforeNowSepartorTime();

            Guid userID = new Guid(strUserID);
            string nowTime = DateTime.Now.ToString("HH:mm:ss");
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TSingleRecord
                        where p.FUserID == userID && Convert.ToDateTime(p.FDate)==DateTime.Now.Date &&string.Compare(p.FTime,beforeTime)>0 && string.Compare(p.FTime,nowTime)<=0
                        select p;
                return q.Count();
            }            
        }

        /// <summary>
        /// 判断指定用户当前阶段是否打过卡【当天打卡时间范围内】
        /// </summary>
        /// <param name="strUserID"></param>
        /// <returns></returns>
        public bool IsCheckedStage(string strUserID)
        {
            return GetStageCheckedCount(strUserID) > 0;
        }



        /// <summary>
        /// 获取指定用户今天所有的打卡记录
        /// </summary>
        /// <returns></returns>
        public List<TSingleRecord> GetSelfTodayCheckedRecord(string strUserID)
        {            
            Guid userID = new Guid(strUserID);
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TSingleRecord
                        where p.FUserID == userID && Convert.ToDateTime(p.FDate)==DateTime.Now.Date
                        orderby p.FTime
                        select p;
                return q.ToList<TSingleRecord>();
            }
        }

        /// <summary>
        /// 获取公司今日打卡记录
        /// </summary>
        /// <returns></returns>
        public List<TSingleRecord> GetTodayAllCheckedRecord()
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TSingleRecord
                        where Convert.ToDateTime(p.FDate) == DateTime.Now.Date
                        orderby p.FTime
                        select p;
                return q.ToList<TSingleRecord>();
            }
        }

        /// <summary>
        /// 查询指定用户指定日期段的打卡记录
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns></returns>
        public List<TSingleRecord> GetMyRecordsByCondition(string userID,string startDate=null,string endDate=null)
        {
            startDate = string.IsNullOrEmpty(startDate) ? DateTime.Today.AddMonths(-1).ToString("yyyy-MM-dd") : startDate;
            endDate = string.IsNullOrEmpty(endDate) ? DateTime.Today.ToString("yyyy-MM-dd") : endDate;
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TSingleRecord
                        where string.Compare(p.FDate,startDate)>=0 && string.Compare(p.FDate,endDate)<=0 &&p.FUserID.Equals(userID)
                        orderby p.FDate ascending, p.FTime ascending
                        select p;
                return q.ToList<TSingleRecord>();
            }
        }

        /// <summary>
        /// 修改打卡备注
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newRemark"></param>
        public void EditRemark(string id, string newRemark)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = db.TSingleRecord.Single(c => c.FID.Equals(id));
                q.FRemark = newRemark;
                q.FRemarkDateTime = TwiDateHelper.GetNowDateTime();
                db.SubmitChanges();
            }
        }
    }
}
